//
//  TTInvitationCodeViewController.m
//  TaoTuo
//
//  Created by admin on 2019/10/16.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTInvitationCodeViewController.h"
#import <UIImageView+WebCache.h>
#import "LLSaveImageTool.h"

@interface TTInvitationCodeViewController ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UIButton *saveBtn;

@end

@implementation TTInvitationCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.fd_prefersNavigationBarHidden = YES;
    self.edgesForExtendedLayout = UIRectEdgeTop;
    self.title = @"邀请二维码";
    
    self.view.backgroundColor = kSeparatorColor;
    
    
    [self setupSubviews];
    @weakify(self)
    [MBProgressHUD showLoadingAnimationOnView:self.view];
    [TTCommonNetData getVipLevelSuccess:^(TTUserModel * _Nonnull userModel) {
        @strongify(self)
        [MBProgressHUD dismissAnimationOnView:self.view];
        @weakify(self)
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:userModel.FriendsQrCodeUrl] placeholderImage:[UIImage imageNamed:@"f_placeholder_img"] options:SDWebImageRetryFailed completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            @strongify(self)
            
            self.saveBtn.hidden = NO;
            
            
        }];
        
    } failure:^{
        [MBProgressHUD dismissAnimationOnView:self.view];
    }];
    
    
    
}

// 保存到相册
- (void)saveBtnClick
{
    [[LLSaveImageTool new] saveImage:self.imageView.image];
}



- (void)setupSubviews
{
    _bgView = [[UIView alloc] init];
    _bgView.backgroundColor = [UIColor whiteColor];
    _bgView.layer.cornerRadius = 10;
    [self.view addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.width.mas_equalTo(300);
        make.height.mas_equalTo(350);
    }];
    
    _imageView = [[UIImageView alloc] init];
    _imageView.contentMode = UIViewContentModeScaleAspectFit;
    [_bgView addSubview:_imageView];
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(25);
        make.centerX.equalTo(self.bgView);
        make.height.width.mas_equalTo(250);
    }];
    
    _saveBtn = [UIButton createButtonWithTitle:@"保存到相册" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:18] backgroundColor:kMainColor target:self action:@selector(saveBtnClick)];
    _saveBtn.hidden = YES;
    _saveBtn.layer.cornerRadius = 5;
    [_bgView addSubview:_saveBtn];
    [_saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bgView).mas_offset(-15);
        make.centerX.equalTo(self.bgView);
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(60);
    }];
    
    
}


@end
